#ifndef _BITSEARCH_H_INCLUDED_
#define _BITSEARCH_H_INCLUDED_

extern const uint16 bit16[];

static inline uint32 bit32(int i)
{
    return i < 16 ? bit16[i] : (uint32)bit16[i-16] << 16;
}

extern const int bitsearch_table[];

static inline int bitsearch(uint32 bitmap)
{
    int r = 0;

    if (!(bitmap & 0xffff)) {
        r += 16;
        bitmap >>= 16;
    }
    if (!(bitmap & 0xff)) {
        r += 8;
        bitmap >>= 8;
    }
    if (!(bitmap & 0xf)) {
        r += 4;
        bitmap >>= 4;
    }

    return r + bitsearch_table[bitmap & 0xf];
}

#endif
